﻿using System;
using System.Collections.Generic;
using System.Linq;
using DAL;
using HomeStayWebCP.Models.BusinessObject;
using HomeStayWebCP.Utility;
using System.Collections;

namespace HomeStayWebCP.Models
{
    public class UserModel
    {
        UsersDataModel model = new UsersDataModel();
        public User SelectUserbyUserName(string username)
        {
            
            return model.SelectUser(username).FirstOrDefault(o => o.Username == username);
        }

        public User SelectUserByEmail(string email)
        {
            return model.SelectUserByEmail(email).FirstOrDefault(o => o.Email == email);
        }
        public void InsertUser(User user)
        {
           
            model.InsertUser(user);
           

        }
        public bool CheckEmailExist(string email)
        {
            return model.CheckEmailExist(email);

        }

        public AuthorInfo GetAuthorOfPost(int postID)
        {
            List<Store_User_GetAuthorOfPost_Result> l = model.GetAuthorOfPost(postID);
            if (l.Count > 0)
            {
                Store_User_GetAuthorOfPost_Result temp = l[0];
                AuthorInfo author = new AuthorInfo();
                author.Email = temp.Email;
                author.Fullname = temp.Fullname;
                author.Phone = temp.Fullname;
               
                author.Username = temp.Username;
                author.AvatarPicture = Util.imageuser + temp.AvatarPicture;
                return author;
            }
            else
            {
                return null;
            }
        }

       //public User SelectUserbyUserId(int userId)
       //{
       //    return model.SelectUserbyUserId(userId);
       //}

       public  User Updateuser(UserObject userobj)
       {
           User user= new User();
           user.Username = userobj.Username;
           user.Fullname = userobj.Fullname;
           user.Password = userobj.Password;//new password
           user.Gender = userobj.Gender;
           user.Phone = userobj.Phone;
           user.Email = userobj.Email;
           user.Address = userobj.Address;
           user.AvatarPicture = userobj.AvatarPicture;
           return model.UpdateUser(user);
       }
       public bool UpdatePassword(string username, string pass)
       {
           return model.UpdatePassword(username, pass);
       }
       public IList<UserInfo> GetListOfUser(int roleID)
       {
           List<Store_User_GetUserOfSystem_Result> listUser = model.GetListOfUser(roleID);
           List<UserInfo> result = new List<UserInfo>();
           if (listUser.Count > 0)
           {
               foreach (var item in listUser)
               {
                   UserInfo i = new UserInfo();
                   i.Email = item.Email;
                   i.Fullname = item.Fullname;
                   i.Phone = item.Phone;
                   i.Role = item.RoleName;
                   i.Username = item.Username;
                   if (item.IsDeleted)
                   {
                       i.Status = "Deleted";
                   }
                   else
                   {
                       i.Status = "Available";
                   }
                   result.Add(i);
               }
               return result;
           }
           else
           {
               
               return null;
           }
       }

       //public IEnumerable SearchUserInSystem(int roleID, string SearchUsername, int SearchRole, int SearchStatus)
       //{

       //    List<Store_User_GetUserOfSystem_Result> listUser = model.SearchUserInSystem(roleID,SearchUsername.Trim(),SearchRole,SearchStatus);
       //    List<UserInfo> result = new List<UserInfo>();
       //    if (listUser.Count > 0)
       //    {
       //        foreach (var item in listUser)
       //        {
       //            UserInfo i = new UserInfo();
       //            i.Email = item.Email;
       //            i.Fullname = item.Fullname;
       //            i.Phone = item.Phone;
       //            i.Role = item.RoleName;
       //            i.UserId = item.UserID;
       //            i.Username = item.Username;
       //            if (item.IsDeleted)
       //            {
       //                i.Status = "Deleted";
       //            }
       //            else
       //            {
       //                i.Status = "Available";
       //            }
       //            result.Add(i);
       //        }
       //        return result;
       //    }
       //    else
       //    {

       //        return null;
       //    }
       //}
       public UserDetailInfo getUserOfSystemDetail(string username)
       {
           //int iID = Int32.Parse(ID);
           Store_User_GetUserOfSystemDetail_Result temp = model.getUserOfSystemDetail(username);
           if (temp != null)
           {
               UserDetailInfo userdetail = new UserDetailInfo();
               userdetail.Username = temp.Username;
               userdetail.Fullname = temp.Fullname;
               userdetail.RoleName = temp.RoleName;
               userdetail.AvatarPicture = Util.imageuser + temp.AvatarPicture;
               userdetail.Gender = temp.Gender;
               userdetail.Email = temp.Email;
               userdetail.Address = temp.Address;
               userdetail.Phone = temp.Phone;
               userdetail.TimeCreated = temp.TimeCreated.ToLongDateString();
               if(temp.IsDeleted)
               {
                   userdetail.Status = "Deleted";
               }
               else
               {
                   userdetail.Status = "Available";
               }
               return userdetail;
           }
           return null;
       }
       public int BanUser(string username)
       {
           return model.BanUser(username);
       }
    }
}